package com.example.safeassistan.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.constants.Constants;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.home.adapter.BackLogAdapter;
import com.example.safeassistan.home.adapter.OverDueTaskAdapter;
import com.example.safeassistan.home.model.MyBackLogListModel;
import com.example.safeassistan.home.model.OverDueTaskListModel;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.utile.JsonUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ActivityOverDueTaskList extends BaseActivity {
    private OverDueTaskAdapter adapter;
    private RecyclerView rcView;
    private SmartRefreshLayout refresh;
    private List<OverDueTaskListModel.ResultDTO.RecordsDTO> data;

    private LinearLayout llNodata;
    private int currentPage=1;
    private int count=0;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ActivityOverDueTaskList.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.backlog_list;
    }

    @Override
    protected void initView() {
        setTitleText("我的任务");
        rcView=findViewById(R.id.rcView);
        refresh=findViewById(R.id.refresh);
        llNodata=findViewById(R.id.llNodata);

        setView();
        getData();
    }


    public void setView(){
        data=new ArrayList<>();
        refresh.setEnableAutoLoadMore(true);//开启自动加载功能（非必须）
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        currentPage = 1;
                        getData();
                    }
                }, 1000);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        currentPage++;
                        getData();
                    }
                }, 1000);
            }
        });
    }

    public void getData(){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("pageNo",currentPage);
            jsonObject.put("pageSize", Constants.PAGESIZE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new HttpRequest(ActivityOverDueTaskList.this).doPost(UrlConstants.checkexpireRecord, null,jsonObject, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean  success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), OverDueTaskListModel.class);
                        if (obj instanceof OverDueTaskListModel) {
                            OverDueTaskListModel  backLogListModel=(OverDueTaskListModel) obj;
                            if (currentPage == 1) {
                                data.clear();
                                count = backLogListModel.getResult().getTotal();
                            if (backLogListModel.getResult().getRecords().size()>0){
                                rcView.setVisibility(View.VISIBLE);
                                llNodata.setVisibility(View.GONE);
                                data=backLogListModel.getResult().getRecords();
                                rcView.setLayoutManager(new LinearLayoutManager(ActivityOverDueTaskList.this));
                                adapter=new OverDueTaskAdapter(data);
                                rcView.setAdapter(adapter);
                                if (adapter.getItemCount() < count) {
                                    //还有多的数据
                                    refresh.finishRefresh();
                                } else {
                                    //没有更多数据（上拉加载功能将显示没有更多数据）
                                    refresh.finishRefreshWithNoMoreData();
                                }
                                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                                    }
                                });
                            }else {
                                rcView.setVisibility(View.GONE);
                                refresh.finishRefreshWithNoMoreData();
                                llNodata.setVisibility(View.VISIBLE);
                            }
                        }else {
                                data.addAll(backLogListModel.getResult().getRecords());
                                adapter.notifyDataSetChanged();
                                if (adapter.getItemCount() < count) {
                                    //还有多的数据
                                    refresh.finishLoadMore();
                                } else {
                                    //没有更多数据（上拉加载功能将显示没有更多数据）
                                    refresh.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                                }
                            }
                            }
                    }else {
                        Toast.makeText(ActivityOverDueTaskList.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return 0;
    }
}